package com.tbit.main.controller;

import com.tbit.common.enums.OperaPswEnum;
import com.tbit.main.constant.OperaPswConstant;
import com.tbit.main.constant.UserConstant;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.MachineBorrow;
import com.tbit.main.pojo.Result;
import com.tbit.main.pojo.User;
import com.tbit.main.service.MachineBorrowService;
import com.tbit.main.service.OperaPswService;
import com.tbit.main.service.UserService;
import com.tbit.manager.api.DepositApplyApi;
import com.tbit.manager.api.dto.DepositApplyDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 押金退款申请
 **/
@Validated
@RestController
@RequestMapping("/depositApply")
public class DepositApplyController {

    @Autowired
    private DepositApplyApi depositApplyApi;
    @Autowired
    private UserService userService;
    @Autowired
    private MachineBorrowService machineBorrowService;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 退押金
     *
     * @param userId   用户id
     * @param remark   备注
     * @param operaPsw 密钥
     */
    @GetMapping("/handleRecharge")
    public Result<String> handleByMan(@NotNull Integer userId, @NotNull String remark, String operaPsw) {
        AccountUser loginUser = LoginUserContextHolder.getLoginUser();

        /* 校验 */
        if (!UserConstant.ACCOUNT_ADMIN.equals(loginUser.getAccountUserType()) &&
                !UserConstant.ACCOUNT_BRAND.equals(loginUser.getAccountUserType())) {
            throw new BaseException("System.noPower");
        }
        /**操作密码校验 兼容5.0.0之前版本 常量校验不通过时，再调接口校验*/
        if (!OperaPswConstant.DEPOSIT_OPERA_UPDATE_MONEY.equals(operaPsw)) {
            operaPswService.checkOperaPsw(loginUser, OperaPswEnum.OPERA_PSW_MONEY_CONFIG.getModule(), operaPsw);
        }

        User user = userService.getByUserId(userId);
        if (user == null) {
            throw new BaseException("user.not.exist");
        }
        MachineBorrow machineBorrow = machineBorrowService.getByUserId(userId);
        if (machineBorrow != null) {
            throw new BaseException("user.have.borrowmachine");
        }

        DepositApplyDTO depositApplyDTO = new DepositApplyDTO();
        depositApplyDTO.setUserId(userId);
        depositApplyDTO.setRemark(remark);
        depositApplyDTO.setReturnTime(new Date());
        depositApplyDTO.setLoginUserAccountUserId(loginUser.getAccountUserId());
        depositApplyDTO.setLoginUserPhone(loginUser.getPhone());
        depositApplyDTO.setLoginUserName(loginUser.getUserName());

        depositApplyApi.manualDepositRefund(depositApplyDTO);

        return Result.success();
    }
}
